import { Navigate, useRoutes } from "react-router-dom";
import KeepAlive from "react-activation";
import { RouteObject } from "@/routers/interface";
import Login from "@/views/login/index";

// * 导入所有router
export const metaRouters = import.meta.globEager("./modules/*.tsx");

// * 处理路由
export const routerArray: RouteObject[] = [];
Object.keys(metaRouters).forEach(item => {
	Object.keys(metaRouters[item]).forEach((key: any) => {
		routerArray.push(...metaRouters[item][key]);
	});
});

// 获取侧边栏的菜单 根据 路由来
export const routerMenu: any[] = routerArray.reduce((init: any, item: any) => {
	if (item.children) {
		init.push({
			path: item.path,
			permissions: item.permissions,
			...(item.meta || {}),
			children: item.children.reduce((init: any, item: any) => {
				init.push({
					path: item.path,
					...(item.meta || {}),
				});
				return init;
			}, []),
		});
	} else {
		init.push({ path: item.path, permissions: item.permissions, ...(item.meta || {}) });
	}
	return init;
}, []).sort((a: any, b: any) => a.sort - b.sort);

// 根据权限过滤筛选出路由
export const filterData = (permissionKey: any) => {
	return routerMenu.reduce((init1: any, item: any, index: any) => {
		if (item.permissions) {
			if (item.permissions.some((iitem: any) => permissionKey.includes(iitem))) {
				init1[0].push({
					...item,
					menuIndex: index,
					children: (item.children || []).reduce((init2: any, iitem: any, iindex: any) => {
						if (permissionKey.includes(iitem.key)) {
							init1[1].push(iitem.path);
							init2.push({
								...iitem,
								menuIndex: iindex,
							});
						}
						return init2;
					}, []),
				});
				init1[1].push(item.path);
			}
		} else {
			init1[0].push({
				...item,
				menuIndex: index,
				children: (item.children || []).reduce((init2: any, iitem: any, iindex: any) => {
					init1[1].push(iitem.path);
					init2.push({
						...iitem,
						menuIndex: iindex,
					});
					return init2;
				}, []),
			});
			init1[1].push(item.path);
		}
		return init1;
	}, [[], []]);
};

export const rootRouter: RouteObject[] = [
	{
		path: "/",
		element: <Navigate to="/home/index" />
	},
	{
		path: "/login",
		element: <Login />,
		meta: {
			requiresAuth: false,
			title: "登录页",
			key: "login",
		}
	},
	...routerArray,
	{
		path: "*",
		element: <Navigate to="/404" />
	}
];

// 缓存页面包一层
export const rootRouters: RouteObject[] = rootRouter.map((item: any) => {
	if (item.children && item.children.length) {
		return {
			...item,
			children: item.children.map((child: any) => {
				if (child.meta && child.meta.keepAlive) {
					const key = child.path;
					return {
						...child,
						element: <KeepAlive id={key} key={key} name={key} saveScrollPosition="screen">{child.element}</KeepAlive>
					}
				}
				return child;
			})
		};
	}
	return {
		...item,
		element: item.meta && item.meta.keepAlive ? <KeepAlive id={item.path} key={item.path} name={item.path} saveScrollPosition="screen">{item.element}</KeepAlive> : item.element
	};
});

const Router = () => {
	const routes = useRoutes(rootRouters);
	return routes;
};

export default Router;
